//定义电影演员相关的接口
const express = require('express')
const router = express.Router()
const response = require('../pool/Response')
//引入mysql连接池
const pool = require('../pool/pool')


const joi = require('joi')

//新增演员列表
router.post('/movie-actor/add',(req,res) => {
    let {actorName,actorAvatar} = req.body
    let schema = joi.object({
        actorName:joi.string().required(),
        actorAvatar:joi.string().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('insert into movie_actor (actor_name,actor_avatar ) values(?,?)',[actorName,actorAvatar],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok())
    })
})


//删除演员
router.post('/movie-actor/del',(req,res) => {
    let {id} = req.body
    //表单验证
    let schema = joi.object({
        id:joi.string().required()
    })
    let {error,value} =schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('delete from movie_actor where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error;
        }
        res.send(response.ok())
    })
})

//查询所有演员接口
router.get('/movie-actors',(req,res) => {
    let {page,pagesize} = req.query
    let schema = joi.object({
        page:joi.number().required(),
        pagesize:joi.number().integer().required()
    })
   let {error,value} = schema.validate(req.query)
    let star = (page-1) * 10
    let size = parseInt(pagesize)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('select * from movie_actor limit ?,?',[star,size],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok(result))
    })
})

 //搜索框查询 
router.post('/movie-actors/name',(req,res) => {
    let {name} = req.body
    let schema = joi.object({
        name:joi.string().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
     res.send(response.error(400,error))
     return;
    }
    pool.query('select * from movie_actor where actor_name like ?',[`%${name}%`],(error,result) => {
        if(error){
            res.send(response.error(response.error(500,error)))
            throw error
        }
        res.send(response.ok(result))
    })
})

//通过电影id查询演员接口
router.get('/movie-actors/movieid',(req,res) => {
    let {movie_id} = req.query
    let schema = joi.object({
        movie_id:joi.string().required()
    })
    let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    let sql = `
    select ma.id actor_id,
    mima.movie_id movie_id,
    ma.actor_name actor_name,
    ma.actor_avatar actor_avatar
    from movie_actor ma join movie_info_map_actor mima on ma.id = mima.actor_id
    where mima.movie_id = ? `
    pool.query(sql,[movie_id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error
        }
        res.send(response.ok(result))
    })
}) 
module.exports = router